﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;

using P2PLib.Client.DataModel;
using P2PLib.Client.Command;

namespace P2PLib.Client.Utils
{
    public class ClientUtils
    {
        public static bool Match(String fileName, String key)
        {
            Regex reg = new Regex(key.ToLowerInvariant());

            return (reg.Match(fileName.ToLowerInvariant())).Success;
        }

        public static List<CFileInfo> SearchLibrary(ObservableCollection<LocalFileInfo> files,string key,SearchType type)
        {
            List<CFileInfo> result=new List<CFileInfo>();
            
            //serach file by hash code;
            if (type.Equals(SearchType.ByHashCode))
            {
                var res = from f in files
                        where f.CFile.FileHashCode.Equals(key)
                        select f.CFile;
                foreach (CFileInfo cf in res)
                {
                    result.Add(cf);
                }
            }

            else
            {
                var res = from f in files
                            where ClientUtils.Match(f.CFile.FileName, key).Equals(true)
                            select f.CFile;  
                foreach (CFileInfo cf in res)
                {
                    result.Add(cf);
                }
            }


            return result;
        }
    }
}
